# -*- coding: utf-8 -*-
"""
@Time: 2022/3/18 08:46
@Author: jins
@File: table_ods_device_electric_ts.py
@Introduction:
"""
import time
from datetime import datetime, timedelta
from threading import Thread

from common.mysqlDataOperate import OperateMysql


def insert(start_device_id, step):
    """
    多线程使用executemany方法插入mysql数据
    @param start_device_id: 起始device_id
    @param step: 步长，等于每个线程需要添加的device_id数量
    @return: None
    """
    om = OperateMysql(version=201)
    data_time = datetime(2022, 4, 1, 0, 0)
    data_time_list = []
    for _ in range(96):
        data_time_list.append(data_time)
        data_time += timedelta(minutes=15)
    device_id_list = [start_device_id + m for m in range(step)]
    # 两个变量device_id和data_time组成笛卡儿积
    # res_list = [(device_id, str(data_time), str(data_time.timestamp())) for device_id in device_id_list for data_time in
    #             data_time_list]
    res_list = [(device_id, str(data_time), str(data_time)) for device_id in device_id_list for data_time in
                data_time_list]
    # print(res_list)
    sql_temp = "INSERT INTO data_center.ods_device_electric_ts224 VALUES(%s,%s,1.234,1.234,1.234,1.234,1.234,1.234,1.234," \
               "1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234," \
               "1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234," \
               "1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234,1.234," \
               "1.234,1.234,1.234,1,1,%s,'mi15')"
    om.excute_many(sql_temp, res_list)
    om.commit()
    om.close()


if __name__ == '__main__':
    start_time = time.time()
    list_device = [i for i in range(10000000, 10000000 + 125000) if i % 1000 == 0]
    print(list_device)
    thread_list = []
    for i in list_device:
        t = Thread(target=insert, args=(i, 1000))
        t.start()
        thread_list.append(t)
    for i in thread_list:
        i.join()
    end_time = time.time()
    res = end_time - start_time
    print(res)
